<?php
/*
    
    Copyright (C)  2012 Sweta ray.
    Permission is granted to copy, distribute and/or modify this document
    under the terms of the GNU Free Documentation License, Version 1.3
    or any later version published by the Free Software Foundation;
    with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
    A copy of the license is included in the section entitled "GNU
    Free Documentation License"
	@license GNU/GPL http://www.gnu.org/copyleft/gpl.html
    Questions for Joomla
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
   
	Questions for Joomla
	Version 0.0.1
	Created date: Sept 2012
	Creator: Sweta Ray
	Email: admin@phpseo.net
	support: support@phpseo.net
	Website: http://www.phpseo.net
*/

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controllerform library
jimport('joomla.application.component.controllerform');
 

class QuestionsControllerQuestion extends JControllerForm
{
	public function save (){
		//TODO: Validate & Sanitize
		//Method to save a question - just calls the parent && sets the redirect
		global $logger;
		$logger->info("QuestionsControllerQuestion::save()");
		
		$data = JRequest::getVar('jform', array(), 'post', 'array');

		if ($data['question']){
			$displayAnswers = 0;
		}
		else {
			$displayAnswers = 1;
		}
		
		//tags -- retrieve the and store them as json
		$tags = $data['tags'];
		if ($tags){
			$tags = explode("," , $tags);
			$tags = json_encode ( $tags );
			$tags = explode(',', $tags);
			$tags = array_map('trim', $tags);
			$tags = implode(',', $tags);
			$tags = str_replace(',""', '', $tags);
			$tags = json_decode($tags);
			//remove duplicates
			$tags = array_values(array_unique($tags));
			$tags = json_encode ( $tags );
			$data['tags'] = $tags;
			//replace the request object
			JRequest::setVar("jform" , $data);
		}
		
		if (parent::save()) {
			$msg = JText::_("MSG_ITEM_SAVED");
			$url = "index.php?option=com_questions&view=questions&answers=" . $displayAnswers;
			$this->setRedirect($url , $msg);
		}		
		
	}
	public function edit() {
		global $logger;
		
		if (!QuestionsHelper::canDo("core.edit")){
			$this->setRedirect("index.php?option=com_questions&view=questions&answers=" . (int)(QuestionsHelper::getActiveSubmenu()=="Answers") , JText::_("MSG_NOAUTH") ,  "error");
			return;
		}
		
		$question = JRequest::getInt("question",1);
		$parent = JRequest::getInt("parent",0);
		$catid = JRequest::getInt("catid", 0);
		
		$app = JFactory::getApplication();
		$app->setUserState("isQuestion", $question);
		$app->setUserState("parentID", $parent);
		$app->setUserState("catid", $catid);
		
		$logger->info ("Item type & parent id, catid set!");
		
		parent::edit();
	}
}
